<?php
class Taikhoan_model extends CI_Model {

    public function __construct(){
        parent::__construct();
    }
	
	function getTaiKhoan() {
    	//$this->load->database();
    	$sql = "SELECT MaTaiKhoan, TenDangNhap, MatKhau, Email, DATE_FORMAT(NgaySinh, '%Y-%m-%d' ) AS NgaySinh, HoTen, DiaChi, SoDienThoai, AnhDaiDien FROM taikhoan";
    	return $this->db->query($sql)->result_array();
    }
	
	function xoataikhoan($matk){
		$sql = "delete from sanpham where masanpham in(select masanpham from dangsanpham where manguoidang in(select manguoidang from nguoidang where mataikhoan = ?))";
		$this->db->query($sql,array($matk));
		
		$sql = "delete from dangsanpham where manguoidang in(select manguoidang from nguoidang where mataikhoan = ?)";
		$this->db->query($sql,array($matk));
			
		$sql = "delete from nguoidang where mataikhoan = ?";
		$this->db->query($sql,array($matk));
		
		$sql = "delete from danhgia where manguoidanhgia in(select manguoidanhgia from nguoidanhgia where mataikhoan = ?)";
		$this->db->query($sql,array($matk));
		
		$sql = "delete from binhluan where manguoidanhgia in(select manguoidanhgia from nguoidanhgia where mataikhoan = ?)";
		$this->db->query($sql,array($matk));
		
		$sql = "delete from nguoidanhgia where mataikhoan = ?";
		$this->db->query($sql,array($matk));
		
		$sql = "delete from taikhoan where mataikhoan = ?";
		$this->db->query($sql,array($matk));
		
		$sql = "delete from bangtam where mataikhoan = ?";
		$this->db->query($sql,array($matk));
	
	}
	
	function getAccount($mataikhoan)
	{
		$qr = "select * from taikhoan where MATAIKHOAN = ?";
		return $this->db->query($qr, array($mataikhoan))->result_array();
	}
	
	function updateAccount($hoten, $diachi, $email, $sodienthoai, $mataikhoan)
	{
		$qr = "	update taikhoan 
				set
					HOTEN = ?,
					DIACHI = ?,
					EMAIL = ?,
					SODIENTHOAI = ?
				where 
					MATAIKHOAN = ?";
		$this->db->query($qr, array($hoten, $diachi, $email, $sodienthoai, $mataikhoan));
	}
	
	function updatePassword($mk_cu, $mk_moi, $mk_moi_re, $mataikhoan)
	{
		$sql = "select * from taikhoan where mataikhoan = ? and matkhau = ?";
		if($this->db->query($sql, array($mataikhoan,sha1($mk_cu)))->num_rows() == 0){
			$_SESSION['thongbao'] = "Mật khẩu cũ không đúng!";
		}else{
			if($mk_moi == $mk_moi_re){
				$qr = "	update taikhoan 
						set
						MATKHAU = ?
						where 
						MATAIKHOAN = ?";
				$this->db->query($qr, array(sha1($mk_moi), $mataikhoan));
				//unset($_SESSION['thongbao']);
				$_SESSION['thongbao'] = "Đổi  mật khẩu thành công!";
			}else{
				$_SESSION['thongbao'] = "Mật khẩu mới không trùng lắp!";
			}
		}		
	}
	
	function verifyUser($u,$pw){
        $this->load->database();
        
		$sql = "select * from admin where tendangnhap=? and matkhau=?";
		$query = $this->db->query($sql,array($u,$pw));
		if($query->num_rows()>0){
			$row = $query->row_array();
			$_SESSION['tenadmin'] = $row['TenDangNhap'];
		}else{
			$_SESSION['error'] = 'Xin lỗi, bạn đã nhập sai user hoặc password!';
		}
	}
    
    //Loc: Kiem tra tai khoan khi dang nhap
    function kiemtrataikhoan()
        {
            $tendangnhap = $this->input->post('tendangnhap');
            $matkhau = sha1($this->input->post('matkhau'));
            
            $query = 'SELECT MaTaiKhoan, TenDangNhap
            FROM taikhoan 
            WHERE taikhoan.TenDangNhap = ? and taikhoan.MatKhau = ?';
            $result = $this->db->query($query, array($tendangnhap, $matkhau)); 
            if($result->num_rows() ==0)
            {
                return -1;
            }
            else
            {
                return $result;
            }
        }  
      
    //Loc: Lay thong tin tai khoan de show ra trang chu   
    function laythongtintaikhoan($mataikhoan)
    {
        $query = 'SELECT taikhoan.TenDangNhap, DiemNguoiDang, DiemNguoiDanhGia, AnhDaiDien
        FROM taikhoan 
        LEFT JOIN nguoidang on (taikhoan.MaTaiKhoan = nguoidang.MaTaiKhoan) 
        LEFT JOIN nguoidanhgia on (taikhoan.MaTaiKhoan = nguoidanhgia.MaTaiKhoan)
        WHERE taikhoan.MaTaiKhoan = ?';
        return $this->db->query($query, array($mataikhoan))->result_array();
    }  
    
    function danganh()
    {
        $config = array('upload_path'   => './images/avatar/',
                        'allowed_types' => 'gif|jpg|png|jpeg',
                        'max_size'      => '200');
        $this->load->library("upload", $config);
        $this->upload->do_upload("anhdaidien");
    }
    
    function taotaikhoan($tendangnhap, $matkhau, $email, $ngaysinh, $hoten, $diachi, $sodienthoai, $anhdaidien)
    {
        $query = 'insert into taikhoan values(null, ?, ?, ?, ?, ?, ?, ?, ? )';
        $this->db->query($query, array($tendangnhap, sha1($matkhau), $email, $ngaysinh, $hoten, $diachi, $sodienthoai, $anhdaidien));
        
		$query = 'select max(MaTaiKhoan) as MaTaiKhoan from taikhoan';
        $tkmoi = $this->db->query($query)->row('MaTaiKhoan');
		
		$query = "insert into bangtam(mataikhoan) values (?)";
		$this->db->query($query, array($tkmoi));
    }
    
    function themnguoidang($mataikhoan)
    {
        $query = 'insert into nguoidang values (null, ?, 0)';
        return $this->db->query($query, array($mataikhoan));
    }
    
    function themnguoidanhgia($mataikhoan)
    {
        $query = 'insert into nguoidanhgia values (null, ?, 0)';
        return $this->db->query($query, array($mataikhoan));
    }
    
    function laytaikhoanmoi()
    {
        $query = 'select max(MaTaiKhoan) as MaTaiKhoan from taikhoan';
        return $this->db->query($query)->row('MaTaiKhoan');
    }
    
    function chitietnguoidang($masp)
    {
        $query = "select TenDangNhap, HoTen, DiaChi, SoDienThoai, Email, AnhDaiDien, DiemNguoiDang
                    from dangsanpham, nguoidang, taikhoan
                    where dangsanpham.MaNguoiDang = nguoidang.MaNguoiDang
                    and nguoidang.MaTaiKhoan = taikhoan.MaTaiKhoan
                    and dangsanpham.MaSanPham = ?";
        return $this->db->query($query, array($masp))->result_array();
    }
    
    function laynguoidanhgia($mataikhoan)
    {
        $query = "select MaNguoiDanhGia from nguoidanhgia where MaTaiKhoan = ?";
        return $this->db->query($query, array($mataikhoan))->row('MaNguoiDanhGia');
    }
}